<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class DischargeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        foreach (range(0, 9) as $item) {
            Schema::create('tests_' . $item, function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('user_id')->default('0')->comment('用户 ID');
                $table->unsignedInteger('unit_id')->default('0')->comment('单位 ID');
                $table->unsignedInteger('question_count')->default('0')->comment('题目数量');
                $table->unsignedInteger('pre_score')->default('0')->comment('每题分值');
                $table->unsignedInteger('test_score')->default('0')->comment('考试得分');
                $table->unsignedInteger('test_time')->default('0')->comment('考试时间（分钟）');
                $table->unsignedTinyInteger('is_all_right')->default('0')->comment('是否满分');
                $table->text('questions')->comment('题目');
                $table->timestamp('submit_at')->nullable()->comment('提交时间');
                $table->timestamps();

                $table->index('user_id');
                $table->index('submit_at');
            });

            Schema::create('answers_' . $item, function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('user_id')->default('0')->comment('用户 ID');
                $table->unsignedInteger('test_id')->default('0')->comment('考试 ID');
                $table->unsignedInteger('question_id')->default('0')->comment('题目 ID');
                $table->string('answer')->default('')->comment('答案');
                $table->unsignedTinyInteger('is_right')->default('0')->comment('是否正确');
                $table->timestamps();

                $table->index('user_id');
                $table->index('test_id');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        foreach (range(0, 9) as $item) {
            Schema::dropIfExists('tests_' . $item);
            Schema::dropIfExists('answers_' . $item);
        }
    }
}
